package com.example.novelsys.controller;

import com.example.novelsys.entity.*;
import com.example.novelsys.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Controller
public class NovelInfoController {
    @Autowired
    CategoriesService categoriesService;
    @Autowired
    NovelService novelService;
    @Autowired
    RatesService ratesService;
    @Autowired
    FavoriteService favoriteService;
    @Autowired
    CharptService charptService;

    @RequestMapping("/toNovelDetail")
    public String queryDetailNovel(Integer novelId,Integer userId, HttpServletRequest req, HttpServletResponse resp) {


        //2、获取该小说信息
        Novel novele = novelService.queryByNovelId(novelId);
        //3 、获取小说评论
        List<Rates> rateList = ratesService.queryByNovelId(novelId);

        //4、获取下载排行小说
        List<Novel> downList=novelService.queryNovelByType(4);
        //5、获取阅读排行小说
        List<Novel> readList=novelService.queryNovelByType(5);

        //获取收藏信息

        int userid= userId==null?0:userId;

        List<Favorites> favoriteList = favoriteService.queryByID( (long) userid );

        //判断用户是否已经收藏该小说
        for(Favorites favorite : favoriteList) {

            if(favorite.getNovelId()== novelId ) {

                req.setAttribute("isFavorite",true);
                break;
            }
        }



        List novelInfo=new ArrayList();
        List rankInfo=new ArrayList();

        novelInfo.add(novele);

        rankInfo.add(downList);
        rankInfo.add(readList);

        req.setAttribute("novelInfo", novelInfo);
        req.setAttribute("rankInfo", rankInfo);
        req.setAttribute("rateList", rateList);

        //获取小说类别
        List<Categories> categoriesList=categoriesService.queryAll();
        req.setAttribute("categoriesList", categoriesList); //单独用于用于菜单栏显示

        return "novelDetail";
    }




//TXT电子书下载
    @RequestMapping("/downLoad")
    @ResponseBody
    public Novel downLoadTxt(Integer novelId)  {
        Novel charptAll = novelService.queryBykey(novelId);

        return  charptAll;
    }

//小说章节列表
    @RequestMapping("/toNovelcharpt")
    public String charptList(Integer novelId,HttpServletRequest req) {

        //2、获取该小说详细
        Novel noveleCharpt = novelService.queryBykey(novelId);

        //查询无数据，则跳转到错误页面提示
//        if(noveleCharpt==null){
//
//            return "error";
//        }
        req.setAttribute("noveleCharpt", noveleCharpt);
        return "readCharpt";
    }

//阅读小说章节内容
    @RequestMapping("/toNovle")
    public String  novelContent(Integer novelId,Integer sort,  HttpServletRequest req) {
        //获取该小说及其相应章节
        Novel novelCharpt = novelService.queryBykey(novelId);
        //获取该小说某章节内容
        Charpt  charptInfo = charptService.queryCharpt(novelId, sort);

        req.setAttribute("novelCharpt", novelCharpt);
        req.setAttribute("charptInfo", charptInfo);
        return  "novel";
    }
}
